package com.yvon.boot.mybatis.search;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
@Data
public class PageQuery extends BaseQuery {


    /**
     * 最大分页大小，如果分页大小大于500，则用500作为分页的大小。防止有人直接传入一个较大的数，导致服务器内存溢出宕机
     */
    protected static final long MAX_PAGE_SIZE = 500;

    /**
     * 页面
     */
    @ApiModelProperty(value = "页码(默认1)")
    private long page = 1L;

    /**
     * 页面大小
     */
    @ApiModelProperty(value = "页面大小(默认10)")
    private long size = 10L;

    public <T> Page<T> buildPage() {
        return (Page<T>) new Page(this.getPage(), this.getSize());
    }

    public void setSize(long size) {
        if (size > MAX_PAGE_SIZE) {
            this.size = MAX_PAGE_SIZE;
            return;
        }
        this.size = size;
    }

    public void setPage(long page) {
        this.page = page;
    }

    public long getPage() {
        return page;
    }

    public long getSize() {
        return size;
    }

}
